<!--tvm票款差异协查-->
<template>
  <div class="JieZhangDanAndSCCengji el-card is-always-shadow el-card__body">
    <avue-crud
      ref="JieZhangDanAndSCCengji"
      :option="option"
      :table-loading="listLoading"
      :data="data"
      :page.sync="page"
      @search-change="handleFilter"
      @selection-change="selectionChange"
      @refresh-change="refreshChange"
      @on-load="getList"
      :cell-style="cellStyle">
      <template slot="menuRight">
        <el-button type="primary"  size="mini" @click="replaceContent()">一键替换</el-button>
        <el-button type="primary"  size="mini" @click="sendStatus">下发</el-button>
        <el-button type="primary"  size="mini" @click="exportData">导出</el-button>
      </template>
      <template slot-scope="scope" slot="lineCodeSearch">
        <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" clearable filterable>
          <el-option
            v-for="(item,index) in lineCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="stationCodeSearch">
        <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" clearable @change="changeStation" filterable>
          <el-option
            v-for="(item,index) in stationCodes"
            :key="index"
            :label="item.name"
            :value="item.code">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="scope" slot="settleTimeSearch">
        <el-date-picker
          v-model="searchForm.settleTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="发生日期">
        </el-date-picker>
      </template>
      <template slot-scope="scope" slot="tvmOrderSearch">
        <el-select v-model="searchForm.tvmOrder" placeholder="请选择" style="width: 150px" filterable>
          <el-option
            v-for="(item,index) in tvmCodes"
            :key="index"
            :label="item.tvmCode"
            :value="item.tvmCode">
          </el-option>
        </el-select>
      </template>
      <template slot="searchMenu" slot-scope="{row,size}">
        <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm);getList()}" icon="el-icon-delete">清空</el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button type="text" v-if="scope.row.assistStatus!='3'" size="small" plain @click.stop="previewEditor(scope.row,scope.row.assistStatus)">预览</el-button>
        <el-button type="text" v-if="scope.row.assistStatus=='3'" size="small" plain @click.stop="previewEditor(scope.row,scope.row.assistStatus)">回馈预览</el-button>
      </template>
    </avue-crud>
    <HuikuiInfo ref="HuikuiInfo" v-if="isShowDia"></HuikuiInfo>
</div>
</template>


<script>
    import {getForm, postForm} from '@/api/gen/gen'
    import moment from "moment";
    import HuikuiInfo from './HuikuiInfo';
    import {
      clearSelect,
      fenExYuangettoFixed,
      getgetStationCodes,
      getLineCodes, getLineName, getStationName,
      getXieChaStatusFormat, setSearchFiledsEmpty,
      xieChaStatus, getStationByCode,getLineAndStationByCode, getSwPmTvmCodeList
    } from "../../../../../../../api/subwayRat/commonCheck";
    import {
      getSwAuItvmOnlineAmountHierarchyDifferenceList,
      replaceSwAuItvmOnlineAmountHierarchyDifference, sendStatusSwAuItvmOnlineAmountHierarchyDifference
    } from "../../../../../../../api/subwayRat/TVMRevenueVarianceAudit.TvmChaYiDXC/tvmCengJiCYXC/tvmCengJiCYXC/itvmJieZhangDanAndSCCengji";
    import {exportDataToExcel} from "../../../../../../../api/subwayRat/exportSelf";
    export default {
      components:{
        HuikuiInfo,
        moment
      },
      data() {
            return {
              isShowDia:false,
              listLoading: true,
              lineCodes:[],
              stationCodes:[],
              tvmCodes:[],
              selectKeys:[], //存放对像
              selectKeyVals:[], //存放值
              data: [],
              searchForm:{
                lineCode:'',
                stationCode:''
              },
              page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条,
                isAsc: false// 是否倒序
              },
              option:{ },
            }
        },
        created() {
        this.getOption();
        },
        methods: {
          setSearchFiledsEmpty,
          refreshChange() {
            this.getList()
          },
          cellStyle({ row, column, rowIndex,columnIndex }) {
            if(12==columnIndex || 13==columnIndex || 14==columnIndex){
              if (row.diffMoney < 0) {
                return {
                  color: "red",
                };
              }else if (row.diffMoney > 0) {
                return {
                  color: "green",
                };
              }
            }
          },
          getList() {
            this.listLoading = true;
            Object.assign(this.searchForm,this.page);
            let param={compareType:2};
            Object.assign(param,this.searchForm);
            getSwAuItvmOnlineAmountHierarchyDifferenceList(param).then((response)=>{
              this.data = response.data.data;
              this.listLoading = false;
              this.page.total=response.data.total;
            });
          },
          selectionChange(list){
            this.selectKeys=list;
            this.selectKeyVals=[];
            for(let i=0;i<list.length;i++){
              this.selectKeyVals.push(list[i].id);
            }
          },
          handleFilter(params, done) {
            Object.assign(this.searchForm,params);
            this.getList()
            done()
          },
          replaceContent(){
            if(this.selectKeys.length<=0 || this.isassistInvestStatus()){
              this.$message.info("请选择已协查数据操作！");
              return;
            }

            let ids=[];
            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].replaceStatus==0){
                ids.push(this.selectKeys[i].id);
              }
            }
            if(ids.length==0){
              this.$message.warning("无有效替换数据！");
              return;
            }
            replaceSwAuItvmOnlineAmountHierarchyDifference({ids:ids.join(","),tableDataSource:2}).then(res=>{
              if(res.data.code==0){
                this.$message.success("操作成功！");
                this.getList();
                clearSelect(this.$refs.JieZhangDanAndSCCengji)
              }else{
                this.$message.error(res.data.msg);
              }
            })
          },
          previewEditor(row,assistStatus){ //回馈预览
            this.isShowDia=true;
            this.$nextTick(()=>{
              this.$refs.HuikuiInfo.tableDataSource=2;
              this.$refs.HuikuiInfo.recallShow(row.id,row,assistStatus,row.replaceStatus);
            })
          },
          replaceContentOne(row){ //一件替换
            this.isShowDia=true;
            this.$nextTick(()=>{
              this.$refs.HuikuiInfo.recallShow(row.id,[row],1);
            })
          },
          async sendStatus(){
            if(this.selectKeys.length<=0){
              this.$message.warning("请选择下发数据！");
              return;
            }

            for(let i=0;i<this.selectKeys.length;i++){
              if(this.selectKeys[i].assistStatus!=1){
                this.$message.warning("请选择未下发数据！");
                return;
              }
            }
            //下发
            let messageLSList = [];
            for(let i=0;i<this.selectKeys.length;i++){
              let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
              messageLSList.push({
                "content":"车站：" + data.data[0].name,
                "lineCode":this.selectKeys[i].lineCode,
                "stationCode":this.selectKeys[i].stationCode
              })
            }
            let p = {
              messageLSList:JSON.stringify(messageLSList),
              title:this.$store.getters.tag.label,
              msgType:"下发",
              type:"JieZhangDanAndSCCengji_001",
              ids:this.selectKeyVals.join(',')
            };
            sendStatusSwAuItvmOnlineAmountHierarchyDifference(p).then(res=>{
              if(res.data.code==0){
                this.$message.success("操作成功！");
                this.getList();
              }else{
                this.$message.error("操作失败！");
              }
            })

          },
          changeLine(v){ //改变线路时
            getgetStationCodes(v).then(res=>{
              this.stationCodes=res;
              this.searchForm.stationCode='';
            })
          },
          changeStation(v){ //改变车站
            let param={};
            param.lineCode=this.searchForm.lineCode;
            param.stationCode=v;
            getSwPmTvmCodeList(param).then(r=>{
              this.tvmCodes=r.data;
            });
          },
          isassistInvestStatus(){ // 是否协查
            for(let i=0;i<this.selectKeys.length;i++){
              let row=this.selectKeys[i];
              if(row.assistStatus==3){
                return false
              }
            }
            return true;
          },
          async exportData(){ // 导出
            this.searchForm.pageNo=1;
            this.searchForm.pageSize=1000000000;
            if(this.selectKeys.length>0){
                exportDataToExcel(this.selectKeys,this.option.column,'SC层级差异待协查');
            }else {
              let res=await getSwAuItvmOnlineAmountHierarchyDifferenceList(this.searchForm);
              let data = res.data.data;
              exportDataToExcel(data,this.option.column,'SC层级差异待协查');
            }
          },
          getOption(){
            let th=this;
            this.option={
              emptyBtn:false,
              editBtn:false,
              delBtn:false,
              addBtn:false,
              border: true,
              index:true,
              indexLabel:'序号',
              page:true,
              align:'center',
              menuWidth:150,
              menuAlign:'center',
              selection: true,
              reserveSelection:false,
              column:[{label:"线路名称",
                "prop":"lineCode",type:'select',search:true,searchslot:true,formatter: function(row, value , label, column){
                  return getLineName(value);
                }},
                {label:"站点名称",
                  "prop":"stationCode",type:'select',search:true,searchslot:true,formatter: function(row, value , label, column){
              return getStationName(row.lineCode,value);
            }},
                {label:"收入日期",
                  "prop":"settleTime",type:"date",format:'yyyy-MM-dd',valueFormat:'yyyy-MM-dd',search:true,searchslot:true},
                {label:"TVM编号",
                  "prop":"tvmOrder",type:'select',search:true,searchslot:true},
                {label:"结账单据充值金额",width:160,
                  // "prop":"depositAmount",formatter:function (row,value , label, column) {
                  "prop":"tvmBillCashRecharge",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'tvmBillCashRecharge');
                  }},
                {label:"结账单据售卖金额",width:160,
                  // "prop":"saleAmount",formatter:function (row,value , label, column) {
                  "prop":"tvmBillCashSellTicket",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'tvmBillCashSellTicket');
                  }},
                {label:"结账单据故障退款金额",width:200,
                  // "prop":"errorRefundAmount",formatter:function (row,value , label, column) {
                  "prop":"tvmBillCashReturn",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'tvmBillCashReturn');
                  }},
                {label:"SC充值金额",width:120, "prop":"beDepositAmount",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'beDepositAmount');
                  }},
                {label:"SC售卖金额", width:120,"prop":"beSaleAmount",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'beSaleAmount');
                  }},
                {label:"SC退款金额",width:120, "prop":"beErrorRefundAmount",formatter:function (row,value , label, column) {
                    return fenExYuangettoFixed(row,'beErrorRefundAmount');
                  }},
                {label:"充值差异金额",width:120, "prop":"chongzhichayi",formatter:function(row, value , label, column){
                    let chaYi = th.getChaYi(row,'tvmBillCashRecharge','beDepositAmount');
                    row.chongzhichayi=chaYi;
                    return fenExYuangettoFixed(row,'chongzhichayi');
                  }},
                {label:"售卖差异金额",width:120, "prop":"shoumaichayi",formatter:function(row, value , label, column){
                    let chaYi = th.getChaYi(row,'tvmBillCashSellTicket','beSaleAmount');
                    row.shoumaichayi=chaYi;
                    return fenExYuangettoFixed(row,'shoumaichayi');
                  }},
                {label:"退款差异金额",width:120,"prop":"guzhangtuikuanchayi",formatter:function(row, value , label, column){
                    let chaYi = th.getChaYi(row,'tvmBillCashReturn','beErrorRefundAmount');
                    row.guzhangtuikuanchayi=chaYi;
                    return fenExYuangettoFixed(row,'guzhangtuikuanchayi');
                  }},
                {label:"协查状态",
                  "prop":"assistStatus",type:'select',search:true,dicData:xieChaStatus(),
                  formatter:function(row, value , label, column){
                    return getXieChaStatusFormat(value);
                  }},
                {label:"下发协查时间",width:120,
                  "prop":"sendAssistTime",formatter:function (row,v) {
                    return moment(v).format("YYYY-MM-DD");
                  }}
              ]
            }
          },
          getLcOrScAmount(originType,needType,thisData){ //结账金额或者 sc
            if((originType+"")==(needType+"")){
              return thisData;
            }else{
              return 0;
            }
          },
          getChaYi(row,compareFirst,compareSec){ //结账金额 sc 差额
            let first=this.getLcOrScAmount('','',row[compareFirst]);//结账金额
            let sec=this.getLcOrScAmount(row.compareType,2,row[compareSec]); //sc
            return first-sec;
          }
        },
      mounted() {
        getLineCodes().then(res=>{ //获取线路
          this.lineCodes=res;
        });
      }
    }
</script>
<style lang="scss">

</style>
